<?php

class Admin_Table_FoodsMenu extends Apk_Db_Table
{
	protected $_name      = 'foods_menu';
    protected $_primary   = 'id';
    protected $_ERROR = '';

    public function GetError()
    {
    	return $this->_ERROR;
    }

    public function GetDataList($page=1, $sort=null, $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'public_date','count_food' => 'count(foods_menu_rel.id_food)', 'is_day_off', 'is_active'))
    		  ->joinLeft('foods_menu_rel', 'foods_menu_rel.id_menu = foods_menu.id', array())
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0')
    		  ->group('foods_menu.id');

		if(count($params) > 0)
		foreach($params as $k => $v)
		{
			switch ($k)
			{
				case 'is_top':
				case 'is_active':
					if(intval($v) >= 0 && trim($v) != '')
					$sel->where($this->_name.'.'.$k.' = ?', intval($v));
					break;
				case 's':
					if(trim($v) != '')
					{
						$sel->where($this->_name.'.title like \'%'.$v.'%\' or '.$this->_name.'.desc like \'%'.$v.'%\'  ');
					}
					break;
				default:
					if(intval($v) > 0)
					$sel->where($this->_name.'.'.$k.' = ?', $v);
			}
		}


		if(!is_null($sort) && strlen($sort) > 0) $sel->order($this->_name.'.'.$sort);
		else $sel->order($this->_name.'.public_date desc');

    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();

    }

    public function GetLastRow()
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('is_ghost = 0')
    				->where('is_removed = 0')
    				->order('public_date desc')
    				->limit(1);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }

    public function GetFoodList($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from('foods_menu_rel', array('foods.id', 'foods.title', 'categories.menu_name_ru'))
    				->joinLeft('foods', 'foods.id = foods_menu_rel.id_food', array())
    				->joinLeft('categories', 'categories.id = foods.id_cat', array())
    				->where('foods_menu_rel.id_menu = ?', $id)
    				->order('foods.title');

    	return $this->setDecorator('simpleDecorator')->getPager($sel, 1, array('perPage' => 1000));
    }

    public function GetFoodIDs($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from('foods_menu_rel')
    				->where('foods_menu_rel.id_menu = ?', $id);

    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();

    	$r = array();

    	if(count($data) > 0)
    	foreach($data as $k => $v) $r[] = $v['id_food'];

    	return $r;
    }



    //*******************
    // insert/update/delete

	public function insert(array $data, $user_id)
    {
    	if(!isset($data['public_date'])) $data['public_date'] = time();
    	$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;

        return parent::insert($data);
    }

    public function update(array $data, $id, $user_id)
    {
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

    	if (isset($data['public_day']))
        {
        	$data['public_date'] = mktime(0, 0, 0, $data['public_month'], $data['public_day'], $data['public_year']);
        	unset($data['public_month']);
        	unset($data['public_day']);
        	unset($data['public_year']);
	        //unset($data['public_hour']);
	        //unset($data['public_min']);
        }

    	if(!isset($data['is_active'])) $data['is_active'] = 0;
    	if(!isset($data['is_day_off'])) $data['is_day_off'] = 0;

		try{
    		parent::update($data, 'id = '.$id);
		}catch(Exception $e)
		{
			echo $e->__toString(); die();
		}

		return true;
    }

    public function update_all(array $data, $id, $user_id)
    {
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

		return parent::update($data, 'id = '.$id);
    }

    public function remove($items, $user_id)
    {
    	foreach($items as $k => $id)
    	{
	    	$data['modify_user_id'] = $user_id;
	    	$data['modify_date'] = time();
	    	$data['is_removed'] = 1;

	    	parent::update($data, 'id = '.$id);
    	}

    	return true;
	}

    public function remove_foods($items, $user_id)
    {
    	foreach($items as $k => $id)
    	{
	    	$this->getAdapter()->delete('foods_menu_rel', 'id_food = '.intval($id));
    	}

    	return true;
	}


	public function add_foods($id, array $data)
    {
    	if(!(intval($id) > 0)) return true;
    	$f['id_menu'] = $id;

    	foreach($data as $id_food)
    	{
    		if(!(intval($id_food) > 0)) continue;
    		$f['id_food'] = intval($id_food);
    		try{
    			$this->getAdapter()->insert('foods_menu_rel', $f);
    		}catch(Exception $e){
    			echo $e->__toString(); die();
    		}
    	}

    	return true;
    }


    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }



}
?>